import { defineStore } from 'pinia'
import request from '@/utils/request'
import { CategoryItem, IApiRes, TopCategory } from '@/types/data'
// 导入默认值
import { topCategory } from '@/store/constants'

const topCategoryList = topCategory.map(i => ({id: i, name: i}))

export default defineStore('catego', {
  state: () => {
    return {
      list: topCategoryList as CategoryItem[],  // 首页导航目录
      topCategory: {} as TopCategory  // 一级目录
    }
  },
  actions: {
    // 首页导航目录
    async getAllCategory() {
      const res = await request.get<IApiRes<CategoryItem[]>>('/home/category/head')
      // console.log(res)
      this.list = res.data.result
    },
    // 一级目录
    async getTopCategory(id: string) {
      const res = await request.get<IApiRes<TopCategory>>('/category', {params: {id}})
      // console.log(res)
      this.topCategory = res.data.result
    }
  }
})